package com.um.jdy.rpc.factory;

import cn.hutool.json.JSONUtil;
import com.um.jdy.rpc.api.RpcEBDeviceService;
import com.um.jdy.rpc.api.RpcEBoxDeviceService;
import com.um.jdy.rpc.api.RpcEVDeviceService;
import com.um.jdy.rpc.api.RpcPBDeviceService;
import com.um.jdy.rpc.api.eb.RpcEBPzMqttDeviceService;
import com.um.jdy.rpc.api.eb.RpcEBXhTCPDeviceService;
import com.um.jdy.rpc.api.eb.RpcEBYdSDKDeviceService;
import com.um.jdy.rpc.api.ebox.RpcEBoxXhTCPDeviceService;
import com.um.jdy.rpc.api.ebox.RpcEBoxPzMqttDeviceService;
import com.um.jdy.rpc.api.pb.RpcPBWsTCPDeviceService;
import com.um.jdy.rpc.api.pb.RpcPBXhTCPDeviceService;
import com.um.jdy.rpc.api.ev.RpcEVYkcDeviceService;
import com.um.jdy.rpc.entity.xh.EBWirelessDeviceParams;
import com.um.jdy.rpc.entity.xh.EBXhDeviceParams;
import com.um.jdy.rpc.entity.xh.PBXhParamsDTO;
import com.um.jdy.rpc.entity.ykc.EVYkcDeviceParams;
import com.um.jdy.rpc.enums.DeviceTypeEnum;
import com.um.jdy.rpc.enums.DeviceProtocolEnum;
import com.um.jdy.rpc.enums.WireEnum;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * @author 邓南辉
 * 日期 2023/5/22
 * 版权所有 广州优匠科技有限公司
 */

@Slf4j
@Component
public class ServiceFactory {

    @DubboReference
    RpcEBXhTCPDeviceService rpcEBXhTCPDeviceService;

    @DubboReference
    RpcEBPzMqttDeviceService rpcEBPzMqttDeviceService;

    @DubboReference
    RpcEBYdSDKDeviceService rpcEBYdSDKDeviceService;

    @DubboReference
    RpcPBXhTCPDeviceService rpcPBXhTCPDeviceService;

    @DubboReference
    RpcEVYkcDeviceService rpcEVYkcDeviceService;

    @DubboReference
    RpcEBoxXhTCPDeviceService rpcEBoxXhTCPDeviceService;

    @DubboReference
    RpcEBoxPzMqttDeviceService rpcEboxPzMqttDeviceService;

    @DubboReference
    RpcPBWsTCPDeviceService rpcPBWsTCPDeviceService;

    /**
     * 根据厂家调用单车桩设备服务
     * @param protocol
     * @return
     */
    public RpcEBDeviceService getRpcEBDeviceService(String protocol) {

        if(StringUtil.isEmpty(protocol)){
            throw new ServiceException("设备需要断电再重新上电！");
        }

        if (DeviceProtocolEnum.XH_TCP_10.getKey().equals(protocol)) {
            return rpcEBXhTCPDeviceService;
        }else if(DeviceProtocolEnum.PZ_MQTT_10.getKey().equals(protocol)){
            return rpcEBPzMqttDeviceService;
        }else if(DeviceProtocolEnum.YD_SDK_10.getKey().equals(protocol)){
            return rpcEBYdSDKDeviceService;
        }

        throw new ServiceException("没有适合设备使用的服务！");
    }

    /**
     * 根据厂家调用汽车桩设备服务
     * @param protocol
     * @return
     */
    public RpcEVDeviceService getRpcEVDeviceService(String protocol) {
        if(StringUtil.isEmpty(protocol)){
            throw new ServiceException("设备需要断电再重新上电！");
        }

        if(DeviceProtocolEnum.YKC_16.getKey().equals(protocol)){
            return rpcEVYkcDeviceService;
        }

        throw new ServiceException("没有适合设备使用的服务！");

    }

    /**
     * 根据厂家调用充电宝设备服务
     * @param protocol
     * @return
     */
    public RpcPBDeviceService getRpcPBDeviceService(String protocol) {
        if(StringUtil.isEmpty(protocol)){
            throw new ServiceException("设备需要断电再重新上电！");
        }

        if (DeviceProtocolEnum.XH_TCP_10.getKey().equals(protocol)) {
            return rpcPBXhTCPDeviceService;
        }else if (DeviceProtocolEnum.WS_TCP_10.getKey().equals(protocol)) {
            return rpcPBWsTCPDeviceService;
        }

        throw new ServiceException("没有适合设备使用的服务！");
    }

    /**
     * 根据厂家调用充电宝设备服务
     * @param protocol
     * @return
     */
    public RpcEBoxDeviceService getRpcEBoxDeviceService(String protocol) {
        if(StringUtil.isEmpty(protocol)){
            throw new ServiceException("设备需要断电再重新上电！");
        }

        if (DeviceProtocolEnum.XH_TCP_10.getKey().equals(protocol)) {
            return rpcEBoxXhTCPDeviceService;
        }else if(DeviceProtocolEnum.PZ_MQTT_10.getKey().equals(protocol)){
            return rpcEboxPzMqttDeviceService;
        }

        throw new ServiceException("没有适合设备使用的服务！");
    }

    /**
     * 返回设备默认参数
     * @param protocol
     * @param ebType
     * @return
     */
    public String getEBDeviceParameters(String protocol, WireEnum ebType){
        String json = "{}";
        if (DeviceProtocolEnum.XH_TCP_10.getKey().equals(protocol) && ebType == WireEnum.Wired){
            EBXhDeviceParams ebParamsDTO = new EBXhDeviceParams();
            ebParamsDTO.setPower(15);
            ebParamsDTO.setMoney(60);
            ebParamsDTO.setTemp(80);
            ebParamsDTO.setOffTime1(30);
            ebParamsDTO.setOffTime2(30);
            json = JSONUtil.toJsonStr(ebParamsDTO);
        }else if (DeviceProtocolEnum.XH_TCP_10.getKey().equals(protocol) && ebType == WireEnum.Wireless){
            EBWirelessDeviceParams ebWirelessDeviceParams = new EBWirelessDeviceParams();
            ebWirelessDeviceParams.setMaxVoltage(57000);
            ebWirelessDeviceParams.setMaxCurrent(3000);
            ebWirelessDeviceParams.setMinCurrent(500);
            ebWirelessDeviceParams.setTemp(80);
            ebWirelessDeviceParams.setValue1(1);
            ebWirelessDeviceParams.setValue2(0);
            json = JSONUtil.toJsonStr(ebWirelessDeviceParams);
        }

        return json;
    }

    /**
     * 返回设备默认参数
     * @param deviceType
     * @param protocol
     * @return
     */
    public String getDeviceParameters(String deviceType, String protocol){
        String json = "{}";
        if(deviceType.equals(DeviceTypeEnum.EBox.name())){
            if (DeviceProtocolEnum.XH_TCP_10.getKey().equals(protocol)){
                EBXhDeviceParams eBoxParamsDTO = new EBXhDeviceParams();
                eBoxParamsDTO.setPower(15);
                eBoxParamsDTO.setMoney(60);
                eBoxParamsDTO.setTemp(80);
                eBoxParamsDTO.setOffTime1(30);
                eBoxParamsDTO.setOffTime2(30);
                json = JSONUtil.toJsonStr(eBoxParamsDTO);
            }
        }else if(deviceType.equals(DeviceTypeEnum.PB.name())){
            if (DeviceProtocolEnum.XH_TCP_10.getKey().equals(protocol)){
                PBXhParamsDTO pbParamsDTO = new PBXhParamsDTO();
                pbParamsDTO.setHeart(60);
                pbParamsDTO.setPortHeart(30);
                json = JSONUtil.toJsonStr(pbParamsDTO);
            }else if (DeviceProtocolEnum.WS_TCP_10.getKey().equals(protocol)){
                PBXhParamsDTO pbParamsDTO = new PBXhParamsDTO();
                pbParamsDTO.setHeart(30);
                pbParamsDTO.setDomain("");
                pbParamsDTO.setPort("");
                json = JSONUtil.toJsonStr(pbParamsDTO);
            }
        }else if(deviceType.equals(DeviceTypeEnum.EV.name())){
            if (DeviceProtocolEnum.XH_TCP_10.getKey().equals(protocol)){
                EVYkcDeviceParams evParamsDTO = new EVYkcDeviceParams();
                evParamsDTO.setUseStatus(0);
                evParamsDTO.setMaxPower(100);
                json = JSONUtil.toJsonStr(evParamsDTO);
            }
        }

        return json;
    }

}
